//
//  NewspaperViewController.m
//  JianChaRiBao
//
//  Created by yuzhou on 13-5-13.
//  Copyright (c) 2013年 wzyk. All rights reserved.
//

#import "NewspaperViewController.h"
#import "NewspaperCell.h"
#import "NewspaperCategoryView.h"
@interface NewspaperViewController ()
{
    BOOL searchNewspaper;

}
@property (nonatomic,retain) NSMutableArray *newspaperArray;

@end

@implementation NewspaperViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.view.frame = self.parentViewController.view.bounds;
    [self getData];
    [datePicker setTimeZone:[NSTimeZone timeZoneWithName:@"Asia/Shanghai +8"]];
}

- (void)getData
{
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        id obj = [[DataRequest sharedRequest] getRecentNewspaper];
        dispatch_async(dispatch_get_main_queue(), ^{
            if ([obj isKindOfClass:[NSDictionary class]] && [[obj objectForKey:@"result"]isKindOfClass:[NSArray class]]) {
                self.newspaperArray = [NSMutableArray arrayWithArray:[obj objectForKey:@"result"]];
                [newspaperTableView reloadData];
            }
        });
    });
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (searchNewspaper) {
        return 1;
    }
    return _newspaperArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentify = @"NewspaperCell";
    NewspaperCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentify];
    if (cell == nil) {
        cell = [[[NewspaperCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentify] autorelease];
    }
    if (searchNewspaper) {
        [cell updateDisplay:_newspaperArray title:[[_newspaperArray objectAtIndex:indexPath.row] objectForKey:@"volume"]];
    }else{
        NSLog(@"dict:%@",[_newspaperArray objectAtIndex:indexPath.row]);
        [cell setCategory:[_newspaperArray objectAtIndex:indexPath.row] title:[[_newspaperArray objectAtIndex:indexPath.row] objectForKey:@"pub_date"]];
    }
    
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 155;
}
- (IBAction)dataButtonClicked:(id)sender {
    
    backgroundView.hidden = NO;
    [UIView animateWithDuration:0.3 animations:^{
        backgroundView.alpha = 0.5;
        datePickerView.hidden = NO;
    }];
}
- (IBAction)completeButtonClicked:(id)sender {
    [UIView animateWithDuration:0.3 animations:^{
        backgroundView.alpha = 0;
        datePickerView.hidden = YES;
    }completion:^(BOOL finished) {
        backgroundView.hidden = YES;
    }];

    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSUInteger unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit ;
    NSDateComponents *dateComponent = [calendar components:unitFlags fromDate:datePicker.date];
    
    int year = [dateComponent year];
    int month = [dateComponent month];
    int day = [dateComponent day];
    
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        id obj = [[DataRequest sharedRequest]getNewspaperInfoWithYear:[NSString stringWithFormat:@"%d",year] mouth:[NSString stringWithFormat:@"%d",month] day:[NSString stringWithFormat:@"%d",day]];
        dispatch_async(dispatch_get_main_queue(), ^{
            NSMutableArray * mArray  = [NSMutableArray arrayWithArray:[obj objectForKey:@"result"]];
            
                
            if (mArray && mArray.count != 0) {
                NSString * code = [[[mArray objectAtIndex:0] objectForKey:@"status_code"] stringValue];
                if (code && code.length != 0 && [code isEqualToString:@"0"]) {
                    ABPromptView * abp  = [[ABPromptView alloc]initWithTitle:@"没有报纸"];
                    [abp show];
                    [abp release];
                }else{
                    [self.newspaperArray removeAllObjects];
                    self.newspaperArray = [NSMutableArray arrayWithArray:mArray];
                    NSLog(@"newsarry:%@",self.newspaperArray);
                    searchNewspaper = YES;
                    [newspaperTableView reloadData];
                }
            }else{
                ABPromptView * abp  = [[ABPromptView alloc]initWithTitle:@"请求超时"];
                [abp show];
                [abp release];
            }
        });
    });

    
    
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
    UITouch * touch = [touches anyObject];
    if (touch.view == backgroundView) {
        [UIView animateWithDuration:0.3 animations:^{
            backgroundView.alpha = 0;
            datePickerView.hidden = YES;
        }completion:^(BOOL finished) {
            backgroundView.hidden = YES;
        }];
    }
}


- (IBAction)pickerValueChanged:(id)sender {
    
}



- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)dealloc {
    [newspaperTableView release];
    [_newspaperArray release];
    [datePickerView release];
    [backgroundView release];
    [datePicker release];
    [super dealloc];
}
- (void)viewDidUnload {
    [newspaperTableView release];
    newspaperTableView = nil;
    [datePickerView release];
    datePickerView = nil;
    [backgroundView release];
    backgroundView = nil;
    [datePicker release];
    datePicker = nil;
    [super viewDidUnload];
}
@end
